/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package system;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.jsp.JspWriter;
import tools.dbconnector;

/**
 *
 * @author Muhammad Anis
 */
public class allover {
    //memiliki beberapa kategori

    List<category> category = new LinkedList<category>();
    
    //fungsi-fungsi yang akan digunakan
    //meload awal

    public allover() {
        load();
    }

    public void insertCategory(category cat) {
        category.add(cat);
    }

    public void insertCategory(List<category> input) {
        if (input != null) {
            category.addAll(input);
        }
    }

    private void load() {
        dbconnector db = new dbconnector();
        insertCategory(db.getAllCategory());
        //load sub kategori
        for (category c : category) {
            c.load();
        }
    }

    //memprint semua isi dari kategori
    public void print(JspWriter out) {
        try {
            for (category c : category) {
                String visible,height,img;
                if(c.visibility.equals("visible")){
                    visible = "visible";
                    height="auto";
                    img="min";
                }
                else{
                    visible = "hidden";
                    height="0px";
                    img="plus";
                }
                out.println("<div  id=\""+c.nama+"_parent"+"\"class=\"category\" >"
                        + "<a href=\"javascript:void(0);\" onclick=\"showHideDiv('"+c.nama+"')\"><img id=\""+c.nama+"_images\""+" src=\"images/"+img+".png\"/></a>"
                        + "<span onclick=\"chooseCategory('"+c.nama+"');\">"+c.nama+"</span></div>");

                out.println("<div  id=\""+c.nama+"\" style=\"visibility:"+visible+";height:"+height+";\">");
                c.print(out);
                out.println("</div>");
            }
        } catch (IOException ex) {
            Logger.getLogger(allover.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    //mengupdate

    public void update(JspWriter out) {
        //setiap kategori akan mengupdate isinya
        dbconnector db = new dbconnector();
        List<category> listCat = db.getAllCategory();
        boolean isAda = false;
        for(category c:listCat){
           if(!isHasCategory(c))
               category.add(c);
        }
        //kategori akan mengupdate subkategorinya
        for(category c:category){
            c.update();
        }
        print(out);
    }

    public boolean isHasCategory(category c){
        for(category ca:category){
            if(ca.nama.equals(c.nama))
                return true;
        }
        return false;
    }

    public category getCategory(String categoryname){
        for(category c : category){
            if(c.nama.equals(categoryname))
                return c;
        }
        return null;
    }

    public subcategory getSubCategory(String subcategoryname){
        subcategory s;
        for(category c:category){
            if((s=c.getSubCategory(subcategoryname))!=null)
                return s;
        }
        return null;
    }

    public void addRoom(String roomname,String moderator,String parent){
        // ga boleh namanya sama antar room,kategori,subkategori
        List<String> temp = getAllCatAndSubCatName();
        if(temp.contains(roomname))
            return;
        //cari dulu di kategori
        category c;
        subcategory s;
        if((c=getCategory(parent))!=null){
            //berarti tambahkan disini
            room r = new room(roomname, moderator);
            c.room.add(r);
        } else if((s=getSubCategory(parent))!=null){
            room r = new room(roomname, moderator);
            s.room.add(r);
        }
        //ga dapet apa2
    }

    public void setVisibility(String status,String nama){
        category c;
        subcategory s;
        if((c=getCategory(nama))!=null){
            //berarti tambahkan disini
            c.setVisibility(status);
        } else if((s=getSubCategory(nama))!=null){
            s.setVisibility(status);
        }
    }
    
    public void addUserinroom(String username,String roomname){
        for(category c:category){
            if(c.addUserInRoom(roomname, username))
                return;
        }
    }

    public void addChatinRoom(String username,String roomaName,String message){
        for(category c:category){
            if(c.addChatinRoom(username, roomaName, message))
                return;
        }
    }
    
    public room getRoom(String roomname){
        for(category c : category){
           room r = c.getRoom(roomname);
           if(r!=null){
               return r;
           }

        }
        return null;
    }

    public void printChat(String roomname,JspWriter out){
        room r = getRoom(roomname);
        if(r!=null){
            r.printChat(out);
        }
    }

    public List<String> getAllCatAndSubCatName(){
        List<String> temp = new LinkedList<String>();
        for(category c: category){
            temp.add(c.nama);
            temp.addAll(c.getAllSubcategoryName());
        }
        return temp;
    }
    
    public boolean isRoomUpdated(String username){
        return getRoom(username).isUpdate();
    }
    
    public List<room> getAllRoom(){
        List<room> ret = new LinkedList<room>();
        for(category c:category){
            ret.addAll(c.getAllRoom());
        }
        return ret;
    }
    
    
}
